package com.example.dmc_android_sellbreakfast_ver1;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button btn_login;
	private TextView tv_register, tv_getpw;
	private EditText edt_user, edt_pw;
	Database mDatabase;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		handle();
	}

	private void init() {
		// TODO Auto-generated method stub
		btn_login = (Button) findViewById(R.id.btnLogin);
		tv_register = (TextView) findViewById(R.id.tv_register);
		tv_getpw = (TextView) findViewById(R.id.tv_notRememberPw);
		edt_user = (EditText) findViewById(R.id.edt_UserName_login);
		edt_pw = (EditText) findViewById(R.id.edt_Password_login);
		mDatabase = new Database(this);
	}

	private void handle() {
		// TODO Auto-generated method stub
		tv_register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent mIntent = new Intent(v.getContext(),
						addNewUserActivity.class);
				startActivity(mIntent);
			}
		});
		tv_getpw.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		btn_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String user = edt_user.getText().toString().trim();
				String pw = edt_pw.getText().toString().trim();
				mDatabase.open();
				boolean kq = mDatabase.kiemTraLogin(user, pw);
				if (kq) {
					Intent mIntent = new Intent(v.getContext(),
							TabhostActivity.class);
					mIntent.putExtra("id_user", user);
					mIntent.putExtra("pw_user", pw);
					startActivity(mIntent);
					Toast.makeText(v.getContext(), "Dang Nhap Thanh Cong", 1000)
							.show();
				} else {
					Toast.makeText(v.getContext(),
							"User or PassWord is not correct", 1000).show();
				}
			}
		});
	}

}
